package cn.acewill.yxd.yunxundian.ui.sign;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.BaseActivity;
import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.adapter.PreviewAdapter;
import cn.acewill.yxd.yunxundian.componet.CheckDataDialog;
import cn.acewill.yxd.yunxundian.model.ChongFuKouFen;
import cn.acewill.yxd.yunxundian.model.Employee;
import cn.acewill.yxd.yunxundian.model.Schedule;
import cn.acewill.yxd.yunxundian.model.reportViewBean;
import cn.acewill.yxd.yunxundian.model.tempLostRR;
import cn.acewill.yxd.yunxundian.ui.MainActivity;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.MEvaLoadModel;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.MEvaluationView;
import cn.acewill.yxd.yunxundian.ui.sign.Contract.PreViewLoadView;
import cn.acewill.yxd.yunxundian.ui.sign.Presenter.PreviewPresenterIml;
import cn.acewill.yxd.yunxundian.utils.CacheActivity;

/**
 * Created by xxx on 2018/2/1.
 */

public class PreViewAcitivity extends BaseActivity implements PreViewLoadView{
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private RecyclerView preRecycle;
    private int schId;
    private PreviewPresenterIml presenterIml;
    private PreviewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preview_eva_view);
        CacheActivity.addActivity(this);
        initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_eva_submit,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.tishi:
                checkSubmit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView(){
        schId = getIntent().getIntExtra("schId",-1);
        if (schId==-1){
            showToast(this,"数据错误");
            finish();
        }
        Toolbar toolbar = findViewById(R.id.myToolBar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        preRecycle = findViewById(R.id.preRecycle);
        TextView title = findViewById(R.id.titleText);
        title.setText("评估预览");
        preRecycle.setLayoutManager(new LinearLayoutManager(this));
        presenterIml = new PreviewPresenterIml(this);
        presenterIml.loadReportViewData(schId);
        presenterIml.loadtomanData(schId);
        presenterIml.loadAreaData(schId);
        presenterIml.loadkouFenData(schId);
    }

    @Override
    public void getTempData(List<reportViewBean> reportList) {
        Log.e("reportList",reportList.toString());
        adapter = new PreviewAdapter(this,reportList);
        preRecycle.setAdapter(adapter);
    }

    @Override
    public void getToManData(List<Employee> employeeList) {
        if (adapter != null){
            adapter.setEmpList(employeeList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getAreaName(String name) {
        if (adapter!=null){
            adapter.setAreaName(name);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getKoufens(List<ChongFuKouFen> kouFens) {
        if (adapter!=null){
            adapter.setChongFu(kouFens);
            adapter.notifyDataSetChanged();
        }
    }


    public void checkSubmit(){
        boolean[] falg = CheckDataDialog.getInstance(this).checkDataComplete(schId);
        if (falg!=null){
            showTipDialog(falg);
        }else{
            showToast(this,"数据错误");
        }
    }

    //未填写数据的提示
    private void showTipDialog(boolean[] need){
        if (!need[0]&&!need[1]&&!need[2]){
            startActivity(new Intent(this,SendMailActivity.class).putExtra("schId",schId));
        }else{
            final Dialog dialog= new Dialog(this,R.style.mydialog);
            View view = LayoutInflater.from(this).inflate(R.layout.eva_check_dialog, (ViewGroup) getWindow().getDecorView(),false);
            TextView addFollow = view.findViewById(R.id.addFollow);
            TextView shopSign = view.findViewById(R.id.shopSign);
            TextView jiBenData = view.findViewById(R.id.jiBenData);
            TextView gotoSubmit = view.findViewById(R.id.gotoSubmit);
            gotoSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    startActivity(new Intent(PreViewAcitivity.this,SendMailActivity.class)
                            .putExtra("schId",schId));
                }
            });
            TextView cancel_action = view.findViewById(R.id.cancel_action);
            cancel_action.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    finish();
                }
            });
            if (!need[0]){
                addFollow.setVisibility(View.GONE);
            }
            if (!need[1]){
                shopSign.setVisibility(View.GONE);
            }
            if (!need[2]){
                jiBenData.setVisibility(View.GONE);
            }
            dialog.setContentView(view);
            dialog.setCancelable(true);
            dialog.show();
        }
    }
}
